﻿using MiniFox.Components;
using MiniFox.Configurations;
using MiniFox.Platform.OAuth2.Configurations;
using MiniFox.Platform.OAuth2.Providers;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace MiniFox.Platform.OAuth2.Models
{

    /// <summary>
    /// 
    /// </summary>
    public class SignNameAttribute : ValidationAttribute
    {
        /// <summary>
        /// 
        /// </summary>
        public SignNameAttribute()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool IsValid(object value)
        {
            if (value != null)
            {
                var provider = ComponentFactory.GetComponent<IOAuth2ConfigProvider>();
                var config = provider.PassportConfiguration;
                string signName = value.ToString();
                Regex loginNameRegex = config.LoginNameRegex;
                Regex regEmailRegex = config.EmailRegex;
                Regex cellphoneRegex = config.CellphoneRegex;

                bool isValid = (config.EnableLoginNameSignin && loginNameRegex.IsMatch(signName));
                isValid |= (config.EnableRegEmailSignin && regEmailRegex.IsMatch(signName));
                isValid |= (config.EnableCellphoneSignin && cellphoneRegex.IsMatch(signName));
                return isValid;
            }

            return false;
        }
    }
}
